# -*- coding: UTF-8 -*-
import pymssql
import _mssql
import json
import requests
import sys
from datetime import date, datetime
from uuid import UUID
import decimal

def __default(obj):
    if isinstance(obj, datetime):
        return obj.strftime('%Y-%m-%d %H:%M:%S')
    elif isinstance(obj, date):
        return obj.strftime('%Y-%m-%d')
    elif isinstance(obj, UUID):
        return str(obj)
    elif isinstance(obj, decimal.Decimal):
        return float(obj)
    else:
        raise TypeError('%r is not JSON serializable' % obj)

reload(sys)   
sys.setdefaultencoding('utf8')


connlock=pymssql.connect(host='.',database='scm_main')
curlock=connlock.cursor()

curlock.execute('update ty_lock set id=1 where id=1')
conn=pymssql.connect(host='.',database='scm_main')
cur=conn.cursor()


dwID=''
with open(sys.path[0]+'/dwid', 'r') as f:
        dwID=f.read()

payload = {'dw':dwID}

r = requests.post("http://wx.nttengyuan.com:81/ty/sync/getGS.do", data=payload)
rs=json.loads(r.text)
print rs['list']

for i, val in enumerate(rs['list']):
            print (val['id'])
            print (val['kh'])
            print (val['user_serial'])
            payload = {'id':val['id']}
            cur.execute("exec ClientPro_Card_loss @user_serial = %d,@card_hao = %s,@bz = '',@lx = 1,@ip = '127.0.0.1',@gly_no = 'admin',@reg_serial = ''",(val['user_serial'],val['kh']))
            requests.post("http://wx.nttengyuan.com:81/ty/sync/updateGS.do", data=payload)
           

conn.commit()
cur.close()
conn.close()         


#if r.status_code==200 and rs['success'] :
    #print u'同步人员记录',len(d),u'条'




connlock.commit()
curlock.close()
connlock.close()



